package com.solidumgame.martillo.hammer
{
	import com.pblabs.engine.entity.EntityComponent;
	import com.solidumgame.martillo.hammer.events.HammerGenericEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.net.URLRequest;
	
	/**
	 * 
	 * @author martin
	 * 
	 */	
	public class HammerSoundComponent extends EntityComponent
	{
		private var sound:Sound;
		private var url:URLRequest;
		
		public function HammerSoundComponent()
		{
			
		}
		
		/**
		 * Se agregan listeners de colision con nail y tableWood.
		 * 
		 */		
		override protected function onAdd():void{
			super.onAdd();
			owner.eventDispatcher.addEventListener(HammerGenericEvent.NAIL_HIT_EVENT,knockSound);
			owner.eventDispatcher.addEventListener(HammerGenericEvent.FLOOR_HIT_EVENT,knockFloorSound);
		}
		
		/**
		 * Se quitan listeners agregados en onAdd. 
		 * 
		 */		
		override protected function onRemove():void{
			super.onRemove();
			owner.eventDispatcher.removeEventListener(HammerGenericEvent.NAIL_HIT_EVENT,knockSound);
			owner.eventDispatcher.removeEventListener(HammerGenericEvent.FLOOR_HIT_EVENT,knockFloorSound);

		}
		
		/**
		 * Manejador de evento de colisión de hammer con nail.
		 * Se ejecuta sound NailHit.mp3.
		 * @param e Evento de Hammer.
		 * 
		 */		
		public function knockSound(e:HammerGenericEvent):void{
			url = new URLRequest("../assets/sounds/NailHit.mp3");
			sound = new Sound(url);
			sound.play(0,0);
		}
		
		/**
		 * Manejador de evento de colisión de hammer con tableWood.
		 * Se ejecuta sound martillo.mp3.
		 * @param e Evento de Hammer.
		 * 
		 */		
		public function knockFloorSound(e:HammerGenericEvent):void{
			url = new URLRequest("../assets/sounds/martillo.mp3");
			sound = new Sound(url);
			sound.play(0,0);
		}
	}
}